package 困难;

import java.util.HashMap;
import java.util.Map;

public class 凑零钱问题 {

    public static Map<Integer,Integer> map = new HashMap<>(6);

    public static void main(String[] args) {
        int[] cions = new int[100];
        for (int i = 0; i < cions.length; i++) {
            cions[i] = i+1;
        }
        long start = System.currentTimeMillis();
        System.out.println(coinChange1(cions,100 ));
        long end = System.currentTimeMillis();
        System.out.println(end-start);

    }

    public static int coinChange(int[] coins,int amount){
        if (map.containsKey(amount)){
            return map.get(amount);
        }
        if (amount==0){
            return 0;
        }
        if (amount<0){
            return -1;
        }
        int res = Integer.MAX_VALUE;
        for (int coin : coins) {
            int subproblem  = coinChange(coins, amount - coin);
            if (subproblem  <0){
                continue;
            }
            res = Math.min(res,1+ subproblem );
        }
        map.put(amount, res==Integer.MAX_VALUE?-1:res);
        return map.get(amount);

    }

    public static int coinChange1(int[] coins,int amount) {
        int[] dp = new int[amount+1];
        dp[0] = 0;
        for (int i = 1; i < dp.length; i++) {
            dp[i] = amount+1;
            for (int coin : coins) {
                if (i-coin<0){
                    continue;
                }
                dp[i] = Math.min(dp[i],1+dp[i-coin] );
            }
        }

        return dp[amount]==amount+1?-1:dp[amount];
       
    }
}
